<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/17
 * Time: 14:52
 */

namespace osc\admin\controller;


use osc\common\controller\AdminBase;
use think\Db;
use think\Loader;
use think\Request;

class DealRecord extends AdminBase
{

    protected function _initialize(){
        parent::_initialize();
        $this->assign('breadcrumb1','操盘管理');
        $this->assign('breadcrumb2','交易记录管理');
    }

    /**
     * 历史成交记录
     * @return mixed
     */
    public function history(){
        $data = $this->request->param();

        $map = [];
        $attribute = '';
        //对手账号
        if(!empty($data['trade_acc'])){
            $map['trade_acc'] = $data['trade_acc'];
        }
        //交易属性
        if(!empty($data['attribute'])){
            $map['attribute'] = ['like','%'.$data['attribute'].'%'];
            $attribute = $data['attribute'];
        }
        //商品名称
        if(!empty($data['variety_id'])){
            $map['variety_id'] = $data['variety_id'];
        }
        //成交日期
        if(!empty($data['deal_date'])){
            $map['deal_date'] = $data['deal_date'];
        }

        $list = Db::name('deal_record')
            ->where($map)
            ->order('id desc')
            ->paginate(10,false, [
            'query' => Request::instance()->param(),//不丢失已存在的url参数
        ]);
        $page = $list->render();

        $info = $list->all();
        foreach($info as &$value){
            $value['attribute'] = str_replace($attribute, '<font color=red>'.$attribute.'</font>',$value['attribute']);//替换 把搜索内容标红
        }

        $var = Db::name('exchage_variety')->select();



        $id_num = Db::name('deal_record')->group('trade_acc')->where($map)->count();

        $this->assign('list',$info);
        $this->assign('id_num',$id_num);
        $this->assign('page',$page);
        $this->assign('var',$var);
        return $this->fetch();
    }

    /**
     * 成交记录表导入
     * @return \think\response\Json
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     */
    public function do_upload(){
        Loader::import("PHPExcel.PHPExcel","",".php");
        Loader::import("PHPExcel.IOFactory","",".php");
        //获取表单上传文件
        $file = request()->file('excel');
        $info = $file->validate(['ext' => 'xlsx,xls'])
                     ->move(ROOT_PATH . 'public' . DS . 'upload' . DS . 'TaoBao');

        //数据为空返回错误
        if(empty($info)){
            $output['code'] = -1;
            $output['info'] = '导入数据格式错误~';
            return json($output);
        }

        //获取文件名
        $exclePath = $info->getSaveName();

        //上传文件的地址
        $filename = ROOT_PATH . 'public' . DS . 'upload' . DS . 'TaoBao'. DS . $exclePath;

        //判断截取文件
        $extension = strtolower( pathinfo($filename, PATHINFO_EXTENSION) );


        //区分上传文件格式
        if($extension == 'xlsx') {
            $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
            $objPHPExcel = $objReader->load($filename, $encode = 'utf-8');
        }else if($extension == 'xls'){
            $objReader =\PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = $objReader->load($filename, $encode = 'utf-8');
        }

        $excel_array = $objPHPExcel->getsheet(0)->toArray();   //转换为数组格式
        array_shift($excel_array);  //删除第一个数组(标题);
        //转换可存储到数据库的格式
        $city = [];
        foreach($excel_array as $k=>&$value) {
            $city[$k]['order_id']       = $value[0];
            $city[$k]['deal_id']        = $value[1];
            $city[$k]['deal_date']      = date('Y-m-d',strtotime($value[2]));
            $city[$k]['deal_time']      = $value[3];
            $city[$k]['variety_id']     = $value[4];
            $city[$k]['variety_name']   = $value[5];
            $city[$k]['deal_price']     = $value[6];
            $city[$k]['deal_num']       = $value[7];
            $city[$k]['deal_money']     = $value[8];
            $city[$k]['trade_acc']      = $value[9];
            $city[$k]['name']           = $value[10];
            //$city[$k]['service']        = $value[11];
            $city[$k]['attribute']      = $value[11];
			$city[$k]['create_time']      = date('Y-m-d',time());
            // print_r($city);exit;
        }
        $res = Db::name('deal_record')->insertAll($city); //批量插入数据
        if($res){
            $output['code'] = 1;
            $output['info'] = '导入数据成功~';
            return json($output);
        }else{
            $output['code'] = 0;
            $output['info'] = '导入数据成功~';
            return json($output);
        }

    }

    /**
     * 删除操作
     */
    public function del(){
        $id = $this->request->param('id');

        $res = Db::name('deal_record')->where('id',$id)->delete();

        if($res){
            $this->redirect('DealRecord/history');
        }else{
            $this->error('删除失败');
        }
    }

}